
#include "bmp.h"

#define virt_printf printf

uint32_t create_bmp_file(const char * file, bmp_info *info)
{
    // 1. create file    
    FILE *fp=fopen(file,"wb");
    if(fp==NULL)
    {
        virt_printf("%s open file fail \n",file);
        return -1;
    }
    
    // 2. file header
    int cnt;
    struct _BMP_HEAD bmp_head;
    memset(&bmp_head,0,sizeof(struct _BMP_HEAD));
    bmp_head.type[0]='B';
    bmp_head.type[1]='M';
    bmp_head.size=54+800*480*3;
    bmp_head.seek=54;
    cnt=fwrite(&bmp_head,1,sizeof(struct _BMP_HEAD),fp);
    virt_printf("成功写入:%d 字节.\n",cnt);

    /*3. 写文件参数信息*/
    struct _BMP_INFO bmp_info;
    memset(&bmp_info,0,sizeof(struct _BMP_INFO));
    bmp_info.size=sizeof(struct _BMP_INFO);
    bmp_info.w=800;
    bmp_info.h=480;
    bmp_info.bit=24;
    bmp_info.flag=1;
    cnt=fwrite(&bmp_info,1,sizeof(struct _BMP_INFO),fp);
    virt_printf("成功写入:%d 字节.\n",cnt);

    /*5. 关闭文件*/
    fclose(fp);
    return 0;
}

uint32_t save_bmp_pix(const char *file, uint32_t width, uint32_t height, uint32_t value)
{

    // 1. open file    
    FILE *fp=fopen(file,"wb");
    if(fp==NULL)
    {
        virt_printf("%s open file fail \n",file);
        return -1;
    }

    // 2. fseek end
    fseek(fp, 0, SEEK_END);

    /*3. 写入位图数据*/
    fwrite(&value,1,3,fp);

    // int w,h;
    // int c=0xFF0033; //红色
    // for(h=0;h<480;h++)
    // {
    //     for(w=0;w<800;w++)
    //     {
    //         fwrite(&c,1,3,fp);
    //     }
    // }

    /*4. 关闭文件*/
    fclose(fp);
    return 0;
}

